Appendix B — Assignment B

Instructions

  1. You may talk to a friend, discuss the questions and potential directions for solving them. However, you need to write your own solutions and code separately, and not as a group activity.

  2. Do not write your name on the assignment.

  3. Insert Code cells in the template provided to write solutions for the assignment. Do not open a new notebook, and work from scratch.

  4. Write your code in the Code cells of the Jupyter notebook. Ensure that the solution is written neatly enough to understand and grade.

  5. Use Quarto to print the .ipynb file as HTML. You will need to open the command prompt, navigate to the directory containing the file, and use the command: quarto render filename.ipynb --to html. Submit the HTML file.

  6. There are 5 points for clealiness and organization. The breakdow is as follows:

  • Must be an HTML file rendered using Quarto (1.5 pts).

  • There aren’t excessively long outputs of extraneous information (e.g. no printouts of unnecessary results without good reason, there aren’t long printouts of which iteration a loop is on, there aren’t long sections of commented-out code, etc.). There is no piece of unnecessary / redundant code, and no unnecessary / redundant text (1 pt)

  • The code follows the python style guide for naming variables, spaces, indentation, etc. (1 pt)

  • The code should be commented and clearly written with intuitive variable names. For example, use variable names such as number_input, factor, hours, instead of a,b,xyz, etc. (1.5 pts)

  1. The assignment is worth 100 points, and is due on 27th Jan 2024 at 11:59 pm.

B.1 Sentence analysis

B.1.1 Word count

Write a function that accepts a word, and a sentence as arguments, and returns the number of times the word occurs in the sentence.

Call the function, and print the returned value if the word is “sea”, and the sentence is “She sells sea shells on the sea shore when the sea is calm.” Note that this is just an example to check your function. Your function should work for any word and sentence.

(10 points)

B.1.2 Max word count

Ask the user to input a sentence. Use the function in B.1.1 to find the word that occurs the maximum number of times in the sentence. Print the word and its number of occurences. If multiple words occur the maximum number of times, then you can print any one of them.

Check your program when the user inputs the sentence, “She sells sea shells on the sea shore when the sea is calm.”. Your program must print, “The word with the maximum number of occurences is ‘sea’ and it occurs 3 times.” Note that this is just an example to check your program. Your program must work for any sentence. If multiple words occur the most, you can print any of them.

(20 points)

B.2 Prime factors

B.2.1 Prime

Write a function that checks if an integer is prime. The function must accept the integer as an argument, and return True if the integer is prime, otherwise it must return False.

Call your function with the argument as 197.

(4 points)

B.2.2 Factor

Write a function that checks if an integer is a factor of another integer. The function must accept both the integers as arguments, and return True if the integer is a factor, otherwise it must return False.

Call your function with the arguments as (19,85).

(3 points)

B.2.3 Prime Factors

Prompt the user to input a positive integer. Use the functions in B.2.1 and B.2.2 to print the prime factors of the integer. Your program should be no more than 4 lines (excluding the comments)

Check your program is the user inputs 190

(8 points)